import Vue from 'vue';
import { messages, t, mergeMessages, createI18n, defaultLocale } from '../locale';

// 全局语言状态管理
const globalState = Vue.observable({
  currentLocale: defaultLocale,
  userI18n: null,
  userMessages: {}
});

const install = (Vue, options = {}) => {
  const { locale = defaultLocale, i18n: userI18n, messages: userMessages = {} } = options;

  // 初始化全局状态
  globalState.currentLocale = locale;
  globalState.userI18n = userI18n;
  globalState.userMessages = userMessages;

  // 创建响应式翻译函数
  const translate = function (key, customLocale) {
    const currentLocale = customLocale || globalState.currentLocale;

    // 如果用户提供了i18n实例，优先使用
    if (globalState.userI18n && typeof globalState.userI18n.t === 'function') {
      try {
        const result = globalState.userI18n.t(key);
        // 如果翻译结果不是key本身，说明找到了翻译
        if (result !== key) {
          return result;
        }
      } catch (e) {
        // 忽略错误，继续使用组件库的翻译
      }
    }

    // 使用组件库的翻译函数
    return t(key, currentLocale, globalState.userMessages);
  };

  // 在Vue原型上添加翻译方法
  Object.defineProperty(Vue.prototype, '$t_', {
    value: translate,
    writable: false,
    configurable: false
  });

  // 添加响应式的当前语言获取方法
  Object.defineProperty(Vue.prototype, '$locale_', {
    get() {
      return globalState.currentLocale;
    },
    configurable: false
  });

  // 添加语言切换方法
  Object.defineProperty(Vue.prototype, '$setLocale_', {
    value: function (newLocale) {
      if (messages[newLocale] || globalState.userMessages[newLocale]) {
        globalState.currentLocale = newLocale;

        // 如果有用户的i18n实例，同时更新它的语言
        if (globalState.userI18n && globalState.userI18n.locale !== undefined) {
          globalState.userI18n.locale = newLocale;
        }

        // 触发所有组件重新渲染
        this.$forceUpdate && this.$forceUpdate();
      } else {
        console.warn(`Language '${newLocale}' is not supported`);
      }
    },
    writable: false,
    configurable: false
  });

  // 添加获取支持的语言列表方法
  Object.defineProperty(Vue.prototype, '$supportedLocales_', {
    get() {
      const componentLocales = Object.keys(messages);
      const userLocales = Object.keys(globalState.userMessages);
      return [...new Set([...componentLocales, ...userLocales])];
    },
    configurable: false
  });

  // 如果用户使用Element UI，可以在这里集成
  if (options.elementUI && typeof window !== 'undefined' && window.ELEMENT) {
    try {
      const ElementUI = window.ELEMENT;
      Vue.use(ElementUI, {
        i18n: (key, value) => translate(key, value)
      });
    } catch (e) {
      console.warn('Element UI integration failed:', e);
    }
  }
};

// 导出语言切换的全局方法
const setGlobalLocale = (newLocale) => {
  if (messages[newLocale] || globalState.userMessages[newLocale]) {
    globalState.currentLocale = newLocale;

    // 如果有用户的i18n实例，同时更新它的语言
    if (globalState.userI18n && globalState.userI18n.locale !== undefined) {
      globalState.userI18n.locale = newLocale;
    }
  } else {
    console.warn(`Language '${newLocale}' is not supported`);
  }
};

// 获取当前语言的全局方法
const getCurrentLocale = () => globalState.currentLocale;

// 获取支持的语言列表的全局方法
const getSupportedLocales = () => {
  const componentLocales = Object.keys(messages);
  const userLocales = Object.keys(globalState.userMessages);
  return [...new Set([...componentLocales, ...userLocales])];
};

// 导出安装函数和工具函数
export default {
  install,
  messages,
  t,
  mergeMessages,
  createI18n,
  defaultLocale,
  setGlobalLocale,
  getCurrentLocale,
  getSupportedLocales
};
export { default as BaseButton } from './BaseButton.vue'
export { default as IconButton } from './IconButton.vue'
export { default as HelloWorld } from './HelloWorld.vue'
export { default as Fuck } from './Fuck.vue'
export { default as MyPlugin } from './plugin'